package com.sansan.qiangji.service.impl.system;

import com.sansan.qiangji.VO.system.menu.MenuVO;
import com.sansan.qiangji.VO.system.menu.MenuPowerVO;
import com.sansan.qiangji.entity.system.Menu;
import com.sansan.qiangji.mapper.system.MenuMapper;
import com.sansan.qiangji.service.inter.system.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {
    private MenuMapper menuMapper;

    @Resource
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    @Override
    public int addMenu(Menu menu) {
        return menuMapper.addMenu(menu);
    }

    @Override
    public List<Menu> findAllMenu() {
        return menuMapper.findAllMenu();
    }

    @Override
    public Menu findByMenuId(Integer menuId) {
        return menuMapper.findByMenuId(menuId);
    }

    @Override
    public List<Menu> findByMenuNode(String menuNode) {
        return menuMapper.findByMenuNode(menuNode);
    }

    @Override
    public List<Menu> findByMenuNodeAndParent(String menuNode, String menuParent) {
        return menuMapper.findByMenuNodeAndParent(menuNode, menuParent);
    }

    @Override
    public List<Menu> findByMenuRoot(String menuRoot) {
        return menuMapper.findByMenuRoot(menuRoot);
    }

    @Override
    public List<Menu> findByMenuParent(String menuParent) {
        return menuMapper.findByMenuParent(menuParent);
    }

    @Override
    public List<Menu> findByMenuRootIn(List<String> menuRoots) {
        return menuMapper.findByMenuRootIn(menuRoots);
    }

    @Override
    public List<MenuVO> getChildrenMenu(List<Menu> menus, String root) {
        List<MenuVO> menuVOS = new ArrayList<>();
        List<Menu> menuList = menus.stream().filter(e -> e.getMenuNode().equals(root)).collect(Collectors.toList());
        String _root = root;
        for (int i = 0; i < menuList.size(); i++) {
            String _root_ = _root;
            Optional<Menu> menuOptional = menuList.stream().filter(e -> e.getMenuParent().equals(_root_)).findFirst();
            if (!menuOptional.isPresent()) {
                break;
            }
            MenuVO menuVO = new MenuVO();
            BeanUtils.copyProperties(menuOptional.get(), menuVO);
            List<MenuVO> menuVOList = getChildrenMenu(menus, menuOptional.get().getMenuRoot());
            if (menuVOList.size() != 0) {
                menuVO.setChildren(menuVOList);
            }
            menuVOS.add(menuVO);
            _root = menuOptional.get().getMenuRoot();
        }
        return menuVOS;
    }

    @Override
    public List<MenuVO> getUserChildrenMenu(List<Menu> menus, String root, List<String> userMenus) {
        List<MenuVO> menuVOS = new ArrayList<>();
        List<Menu> menuList = menus.stream().filter(e -> e.getMenuNode().equals(root)).collect(Collectors.toList());
        String _root = root;
        for (int i = 0; i < menuList.size(); i++) {
            String _root_ = _root;
            Optional<Menu> menuOptional = menuList.stream().filter(e -> e.getMenuParent().equals(_root_)).findFirst();
            if (!menuOptional.isPresent()) {
                break;
            }
            List<String> userMenuList = userMenus.stream().filter(e -> e.equals(menuOptional.get().getMenuRoot())).collect(Collectors.toList());
            if (!menuOptional.get().getMenuStatus() || userMenuList.size() == 0) {
                _root = menuOptional.get().getMenuRoot();
                continue;
            }
            MenuVO menuVO = new MenuVO();
            BeanUtils.copyProperties(menuOptional.get(), menuVO);
            List<MenuVO> menuVOList = getUserChildrenMenu(menus, menuOptional.get().getMenuRoot(), userMenus);
            if (menuVOList.size() != 0) {
                menuVO.setChildren(menuVOList);
            }
            menuVOS.add(menuVO);
            _root = menuOptional.get().getMenuRoot();
        }
        return menuVOS;
    }

    @Override
    public List<MenuPowerVO> getMenuChildrenPower(List<Menu> menus, String root, List<String> menuPowers) {
        List<MenuPowerVO> menuPowerVOS = new ArrayList<>();
        List<Menu> menuList = menus.stream().filter(e -> e.getMenuNode().equals(root)).collect(Collectors.toList());
        String _root = root;
        for (int i = 0; i < menuList.size(); i++) {
            String _root_ = _root;
            Optional<Menu> menuOptional = menuList.stream().filter(e -> e.getMenuParent().equals(_root_)).findFirst();
            if (!menuOptional.isPresent()) {
                break;
            }
            MenuPowerVO menuPowerVO = new MenuPowerVO();
            List<String> menuPowerList = menuPowers.stream().filter(e -> e.equals(menuOptional.get().getMenuRoot())).collect(Collectors.toList());
            if (!menuOptional.get().getMenuStatus()) {
                _root = menuOptional.get().getMenuRoot();
                continue;
            }
            if (menuPowerList.size() == 0) {
                menuPowerVO.setIsEnable(false);
            } else {
                menuPowerVO.setIsEnable(true);
            }
            BeanUtils.copyProperties(menuOptional.get(), menuPowerVO);
            List<MenuPowerVO> menuPowerVOList = getMenuChildrenPower(menus, menuOptional.get().getMenuRoot(), menuPowers);
            if (menuPowerVOList.size() != 0) {
                menuPowerVO.setChildren(menuPowerVOList);
            }
            menuPowerVOS.add(menuPowerVO);
            _root = menuOptional.get().getMenuRoot();
        }
        return menuPowerVOS;
    }

    @Override
    public int updateMenu(Menu menu) {
        return menuMapper.updateMenu(menu);
    }

    @Override
    public int updateMenus(List<Menu> menus) {
        return menuMapper.updateMenus(menus);
    }

    @Override
    public int deleteByMenuId(List<Menu> menus) {
        return menuMapper.deleteByMenuId(menus);
    }
}
